from flask import Blueprint,request,g,jsonify
from modle import *
import re
user_bp=Blueprint('user',__name__,url_prefix='/user')


@user_bp.route('/')
def pop():
    return "我一定要学好django程序"

@user_bp.route('/tt',methods=["post"])
def rog():
    data = request.json
    if not data:
        return {'smg': '字段不完整'}
    name = data.get('name')
    pwd = data.get('pwd')
    pwd1 = data.get('pwd1')
    money = data.get('money')
    if not all([name, pwd,pwd1,money]):
        return jsonify({'code': 400, 'msg': '参数错误'})
    if not re.findall(r'[1-3]\d{9}$', pwd):
        return jsonify({'code': 400, 'msg': '格式不正确'})
    u = User(name=name, pwd=pwd,pwd1=pwd1,money=money)
    db.session.add(u)
    db.session.commit()
    return jsonify({'code': 200, 'msg': '注册成功'})

@user_bp.route('/long',methods=["get"])
def rog1():
    data = request.json
    if not data:
        return {'smg': '字段不完整'}
    name = data.get('name')
    pwd = data.get('pwd')
    if not all([name, pwd]):
        return jsonify({'code': 400, 'msg': '参数错误'})
    if not re.findall(r'[1-3]\d{9}$', pwd):
        return jsonify({'code': 400, 'msg': '格式不正确'})
    u = User(name=name, pwd=pwd)
    db.session.add(u)
    db.session.commit()
    return jsonify({'code': 200, 'msg': '登入成功'})

